package com.pixel.game.entity;

import com.badlogic.gdx.math.Vector2;

public class FriendlyUnit extends Unit {
    public static final float UNIT_SPEED = 100.0f;
    private float attackRange = 60f; // 攻击范围

    private int killCount;
    private int level;
    private static final int KILLS_TO_UPGRADE = 5; // 升级所需的击杀数量
    private static final int LEVELS_TO_SPAWN_NEW_UNIT = 5; // 生成新单位所需的等级
    private float maxHealth;

    private boolean spawnNewUnit;

    public static final float ATTACK_COOLDOWN = 500; // 攻击间隔 500ms

    public FriendlyUnit(Vector2 position, float attackPower, float health, float attackCooldown) {
        super(position, attackPower, health, attackCooldown);
        this.killCount = 0;
        this.level = 1; // 初始等级
        this.maxHealth = health;
    }

    public void incrementKillCount() {
        killCount++;
        if (killCount >= KILLS_TO_UPGRADE) {
            upgradeUnit();
            killCount = 0;
        }
    }

    private void upgradeUnit() {
        // 提升最大血量
        float healthIncrease = 10.0f;
        this.maxHealth += healthIncrease;

        // 恢复满血
        this.setHealth(this.maxHealth);

        // 提升攻击力
        this.setAttackPower(this.getAttackPower() + 5.0f);

        // 提升攻击范围
        this.setAttackRange(this.getAttackRange() + 2f);

        // 提升等级
        this.level++;

        System.out.println("Unit upgraded to level " + this.level + "! New Max Health: " + this.maxHealth + ", New Attack Power: " + this.getAttackPower());

        // 检查是否达到生成新单位的等级
        if (this.level % LEVELS_TO_SPAWN_NEW_UNIT == 0) {
            spawnNewUnit = true;
        }
    }


    public float getMaxHealth() {
        return maxHealth;
    }

    public int getLevel() {
        return level;
    }

    public boolean getSpawnNewUnit() {
        return spawnNewUnit;
    }

    public void setSpawnNewUnit(boolean spawnNewUnit) {
        this.spawnNewUnit = spawnNewUnit;
    }

    public float getAttackRange() {
        return attackRange;
    }

    public void setAttackRange(float attackRange) {
        this.attackRange = attackRange;
    }
}
